package com.rzgeo.wcgallay.comp;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CalendarView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TimePicker;

import com.esri.arcgisruntime.mapping.view.AtmosphereEffect;
import com.esri.arcgisruntime.mapping.view.Camera;
import com.esri.arcgisruntime.mapping.view.LightingMode;
import com.esri.arcgisruntime.mapping.view.SceneView;
import com.larswerkman.holocolorpicker.ColorPicker;
import com.larswerkman.holocolorpicker.SVBar;
import com.rzgeo.wcgallay.R;
import com.rzgeo.wcgallay.util.WcmUtil;

import java.util.Calendar;

/**
 * Created by zhanghang on 2017/12/31.
 */
//TODO 3D效果
public class Wcm3DConf extends LinearLayout {

    private SceneView _sceneView;

    RadioGroup _rgLightingMode;
    RadioButton _rgLightingModeLight;
    RadioButton _rgLightingModeLightShadows;
    RadioButton _rgLightingModeNone;

    RadioGroup _rgAtmosphereEffect;

    RadioButton _rgAtmosphereEffectReal;
    RadioButton _rgAtmosphereEffectHorizon;
    RadioButton _rgAtmosphereEffectNone;
    private CalendarView _cvDate;
    private ColorPicker _pkColor;
    private SVBar _svBar;
    private TimePicker _tpTime;

    public Wcm3DConf(Context context) {
        super(context);
    }

    public Wcm3DConf(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public Wcm3DConf(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }



    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        _cvDate = findViewById(R.id.cv_date);
        _cvDate.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView calendarView, int year, int month, int dayOfMonth) {
                if(_sceneView==null)
                    return;
                else{
                    Calendar c = Calendar.getInstance();
                    c.set(year,month,dayOfMonth);
                    long t = c.getTime().getTime();
                    calendarView.setDate(t);
                    updateTimeSelect();
                }
            }
        });
        _tpTime=findViewById(R.id.tp_time);
        _tpTime.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker timePicker, int hourOfDay, int minute) {
                if(_sceneView==null)
                    return;
                else
                    updateTimeSelect();

            }
        });

        _svBar = findViewById(R.id.svbar);

        _pkColor = findViewById(R.id.pk_color);
        _pkColor.setOnColorSelectedListener(new ColorPicker.OnColorSelectedListener() {
            @Override
            public void onColorSelected(int color) {
                _sceneView.setAmbientLightColor(color);
            }
        });
        _pkColor.addSVBar(_svBar);

        _rgLightingMode = findViewById(R.id.rg_lighting_mode);
        _rgLightingModeLight = findViewById(R.id.rb_lighting_mode_light);
        _rgLightingModeLightShadows = findViewById(R.id.rb_lighting_mode_light_shadows);
        _rgLightingModeNone = findViewById(R.id.rb_lighting_mode_light_none);
        _rgLightingMode.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(_sceneView==null)
                    return;

                if(checkedId==_rgLightingModeLight.getId())
                    _sceneView.setSunLighting(LightingMode.LIGHT);
                else if(checkedId==_rgLightingModeLightShadows.getId())
                    _sceneView.setSunLighting(LightingMode.LIGHT_AND_SHADOWS);
                else
                    _sceneView.setSunLighting(LightingMode.NO_LIGHT);
            }
        });

        _rgAtmosphereEffect = findViewById(R.id.rg_atmosphere_effect);
        _rgAtmosphereEffectReal = findViewById(R.id.rb_atmosphere_effect_real);
        _rgAtmosphereEffectHorizon = findViewById(R.id.rb_atmosphere_effect_horizon);
        _rgAtmosphereEffectNone = findViewById(R.id.rb_atmosphere_effect_none);
        _rgAtmosphereEffect.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(_sceneView==null)
                    return;

                if(checkedId==_rgAtmosphereEffectReal.getId())
                    _sceneView.setAtmosphereEffect(AtmosphereEffect.REALISTIC);
                else if(checkedId==_rgAtmosphereEffectHorizon.getId())
                    _sceneView.setAtmosphereEffect(AtmosphereEffect.HORIZON_ONLY);
                else
                    _sceneView.setAtmosphereEffect(AtmosphereEffect.NONE);
            }
        });
    }

    public boolean isValid(){
        return _sceneView!=null;
    }

    private void updateTimeSelect() {

        Calendar date = Calendar.getInstance();
        WcmUtil.Log.Msg("date:"+_cvDate.getDate());
        date.setTimeInMillis(_cvDate.getDate());

        date.set(Calendar.HOUR_OF_DAY,_tpTime.getCurrentHour());
        date.set(Calendar.MINUTE,_tpTime.getCurrentMinute());

        WcmUtil.Log.Msg(date.get(Calendar.YEAR)+" "+date.get(Calendar.DATE)+date.get(Calendar.HOUR_OF_DAY)+date.get(Calendar.MINUTE));

        _sceneView.setSunTime(date);
    }

    public void setup(SceneView sceneView) {
        _sceneView = sceneView;

        //环境光
        int color = _sceneView.getAmbientLightColor();
        _pkColor.setColor(color);
        //大气效果
        AtmosphereEffect effect = _sceneView.getAtmosphereEffect();
        switch (effect) {
            case NONE:
                _rgAtmosphereEffectNone.setChecked(true);
                break;
            case HORIZON_ONLY:
                _rgAtmosphereEffectHorizon.setChecked(true);
                break;
            case REALISTIC:
                _rgAtmosphereEffectReal.setChecked(true);
                break;
        }

        //日光效果
        LightingMode mode = _sceneView.getSunLighting();
        switch (mode) {
            case NO_LIGHT:
                _rgLightingModeNone.setChecked(true);
                break;
            case LIGHT:
                _rgLightingModeLight.setChecked(true);
                break;
            case LIGHT_AND_SHADOWS:
                _rgLightingModeLightShadows.setChecked(true);
                break;
        }
        //时间
        Calendar date = _sceneView.getSunTime();
        if (date == null) {
            long time = System.currentTimeMillis();
            date = Calendar.getInstance();
            date.setTimeInMillis(time);
        }
        _cvDate.setDate(date.getTimeInMillis());

        _tpTime.setCurrentHour(date.get(Calendar.HOUR_OF_DAY));
        _tpTime.setCurrentMinute(date.get(Calendar.MINUTE));
    }
}
